#include <head.h>

typedef void *(*Fork_f)(void *); // 函数指针

typedef struct str_fork
{
    int pid;
    int id;
    char name[128];
} str_fork;

int fork_func(Fork_f func, void *data)
{
    if (NULL == func)
    {
        return -1;
    }
    int pid = fork();
    func(data); // 回调函数
    if (pid == -1)
    {
        perror("fork");
        exit(1);
    }
    if (pid == 0)
    {
        printf("子进程\n");
    }
    if (pid > 0)
    {
        printf("父进程\n");
        return pid;
    }
    return 0;
}

void *func_1(void *data)
{
    str_fork *S_2 = (str_fork *)data;
    printf("pid = %d\n", S_2->pid);
    printf("id = %d\n", S_2->id);
    printf("name = %s\n", S_2->name);
    return NULL;
}

int main(int argc, char const *argv[])
{
    str_fork S_1 = {1, 2, "hello"};
    fork_func(func_1, &S_1);
    return 0;
}
